﻿using System;
using System.Collections;
using System.Linq;
using Facebook.Api;

namespace Facebook.Configuration
{
    public class FeedTemplateElement : IEquatable<TemplateBundle>
    {
        public String Name { get; set; }

        public String OneLineTemplate { get; set; }

        public StoryTemplateElement ShortStory { get; set; }

        public FeedActionLinkElement ActionLink { get; set; }

        #region [ IEquatable<TemplateBundle> Members ]

        public Boolean Equals(TemplateBundle other)
        {            
            String otherOneLine = other.OneLineStoryTemplates.FirstOrDefault();
            Boolean otherHasShortStory = other.ShortStoryTemplates != null && other.ShortStoryTemplates.Count > 0;
            ShortStoryTemplate shortStory = other.ShortStoryTemplates.FirstOrDefault();
            String otherShortStoryTitle = null;
            String otherShortStoryBody = null;
            if(shortStory != null)
            {
                otherShortStoryTitle = shortStory.TemplateTitle;
                otherShortStoryBody = shortStory.TemplateBody;
            }
            Boolean otherHasActionLink = other.ActionLinks != null && other.ActionLinks.Count > 0;
            ActionLink otherActionLink = other.ActionLinks.FirstOrDefault();
            String otherActionLinkText = null;
            String otherActionLinkHref = null;
            if(otherActionLink != null)
            {
                otherActionLinkText = otherActionLink.Text;
                otherActionLinkHref = otherActionLink.Href;
            }

            return this.OneLineTemplate == other.OneLineStoryTemplates[0]
                && this.ShortStory == null ? otherHasShortStory : (otherShortStoryTitle == this.ShortStory.Title && otherShortStoryBody == this.ShortStory.Body)
                && this.ActionLink == null ? otherHasActionLink : (otherActionLinkText == this.ActionLink.Text && otherActionLinkHref == this.ActionLink.Href);
        }

        #endregion
    }
}